package br.com.loja.view.swing;

import br.com.loja.model.entity.Switchable;
import br.com.loja.model.service.factory.ServiceFactory;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.Component;
import java.lang.reflect.Field;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @brief Classe EntityEditor
 * @author David Luiz
 * @date 14/12/2014
 */
public class ComponentToManyFactoryOld extends AbstractComponentFactory {

    public ComponentToManyFactoryOld(Field field, ServiceFactory serviceFactory) {
        super(field, serviceFactory);
    }

    @Override
    protected Component buildCompositionComponent(Field field, ServiceFactory serviceFactory) {

        Class<?> type = getClassType(field);

        JPanel panel;
        PanelEntity panelEntity = new PanelEntity(serviceFactory);
        panel = panelEntity.getPanel((Class<? extends Switchable>) type);
        panel.setName(field.getName());

        return panel;

    }

    @Override
    protected Component buildAggregationComponent(Field field, ServiceFactory serviceFactory) {
        return null;
    }

    @Override
    protected Component buildEnumComponent(Field field, ServiceFactory serviceFactory) {
        return null;
    }

    @Override
    protected Component buildCommonComponent(Field field, ServiceFactory serviceFactory) {

        Class<?> type = getClassType(field);

        JPanel panel = new JPanel();
        panel.setName(field.getName());

        DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(""));
        builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        builder.appendColumn("right:pref");
        builder.appendColumn("3dlu");
        builder.appendColumn("fill:max(pref; 180px)");

        if (Boolean.class.isAssignableFrom(type)) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setName(field.getName());
            builder.append(field.getName(), checkBox);

        } else {
            JTextField textField = new JTextField(25);
            textField.setName(field.getName());
            builder.append(field.getName(), textField);
        }

        return builder.getPanel();
    }

}
